﻿using AutoMapper;
using B.S.AuthCenter.Api.Read.Application.Command.Department;
using B.S.AuthCenter.Api.Read.DTOs;
using B.S.AuthCenter.Domain;
using B.S.AuthCenter.ErrorCount;
using B.S.AuthCenter.InfrasIntructure.Interface;
using MediatR;

namespace B.S.AuthCenter.Api.Read.Application.Handler.Department
{
    public class GetDepartmentHandler : IRequestHandler<GetDepartmentCommand, ApiResult<List<UsersTreeDTO>>>
    {
        IBaseRepository<UserModel> userRepo;
       


        public GetDepartmentHandler(IBaseRepository<UserModel> userRepo)
        {
            this.userRepo = userRepo;
          
        }

        public Task<ApiResult<List<UsersTreeDTO>>> Handle(GetDepartmentCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<UsersTreeDTO>> res=new ApiResult<List<UsersTreeDTO>>();
            var list=userRepo.GetAll();
            if(request.DepartmentId!=0)
            {
                list = list.Where(x => x.DepartmentId == request.DepartmentId);
            }
            var tree=new List<UsersTreeDTO>();
            foreach (var item in list)
            {
                tree.Add(new UsersTreeDTO
                {
                    id= item.Id,
                    label=item.UserName
                });
            }
            res.Code = ApiEnum.Success;
            res.Msg = "部门树形成功";
            res.Datas = tree;
            return Task.FromResult(res);
        }
    }
}
